package br.com.bluetoothserver.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DAOMySQL 
{
	
	private static String host = "localhost:3306";
	private static String user = "root";
	private static String pass = "admin";
	private static String base = "sv_bluetooth";
	
	private String     url  = null;
	private Connection con  = null;
	private Statement  stm  = null;
	
	public DAOMySQL()
	{
		registerDriver();
		createCon();
		createStatement();
	}
	
	public void createCon(){
		
		url = "jdbc:mysql://"+host+"/"+base;
		try{
			con = DriverManager.getConnection( url, user, pass );
		} 
		catch ( SQLException e ){
			e.printStackTrace();
		}
	}
	
	public void registerDriver(){
		
		try{
			Class.forName( "com.mysql.jdbc.Driver" );
		}
		catch ( ClassNotFoundException   e){
			e.printStackTrace();
		}
		
	}
	
	public void createStatement(){
		try{
			stm = con.createStatement();
		}
		catch ( SQLException e ){
			e.printStackTrace();
		}
	}
	

	
	// Update
	public int executeUpdate( String sql )
	{
		try{
			int response = stm.executeUpdate(sql);
			stm = con.createStatement();
			return response;
		}
		catch ( SQLException e ){
			e.printStackTrace();
		}
		return -1;
	}
	
	public java.sql.ResultSet executeQuery( String sql ){
		try{
			ResultSet rs = stm.executeQuery(sql);
			stm = con.createStatement();
			return rs;
		}
		catch ( SQLException  e ){
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void close(){
		try{
			stm.close();
			con.close();
		} 
		catch ( SQLException e ){
			e.printStackTrace();
		}
	}


}
